package com.abe.mvp.view.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.abe.config.SettingParam;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.view.activity.FragmentCallbackListener.SettingCallbackListener;
import com.abe.mvp.view.adapter.SettingMenuAdapter;
import com.abe.mvp.view.fragment.base.BaseSettingFragment;
import com.abe.threadmill.R;

public class SettingTiyanFragment extends BaseSettingFragment implements
		OnItemClickListener {

	@SuppressWarnings("unused")
	private SettingCallbackListener callback;

	private ListView tiyan_list_type;
	private List<MenuBean> menuBeans;
	private SettingMenuAdapter adapter;
	private boolean isYes;

	private String[] ty = { SettingParam.getStrigById(R.string.common_yes),
			SettingParam.getStrigById(R.string.common_no) };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		callback = (SettingCallbackListener) getActivity();
		View view = this.onCreateView(inflater,
				R.layout.activity_set_fragment_tiyan, container,
				savedInstanceState);
		initView(view);
		return view;
	}

	public void initView(View view) {
		tiyan_list_type = (ListView) view.findViewById(R.id.tiyan_list_type);
		menuBeans = new ArrayList<MenuBean>();
		isYes = SettingParam.getSettingBean().isSupportTourist();
		for (int i = 0; i < ty.length; i++) {
			MenuBean bean = new MenuBean();
			bean.setNameStr(ty[i]);
			if ((isYes && i == 0) || (!isYes && i == 1)) {
				bean.setSelected(true);
			} else {
				bean.setSelected(false);
			}
			menuBeans.add(bean);
		}
		adapter = new SettingMenuAdapter(getActivity(), menuBeans, scale,
				SettingMenuAdapter.TYPE_LIST);
		tiyan_list_type.setAdapter(adapter);
		tiyan_list_type.setOnItemClickListener(this);
	}

	@Override
	protected void saveChange() {
		for (int i = 0; i < menuBeans.size(); i++) {
			MenuBean bean = menuBeans.get(i);
			bean.setSelected(!bean.isSelected());
			menuBeans.set(i, bean);
		}
		adapter.setListData(menuBeans);
		tiyan_list_type.setAdapter(adapter);
		SettingBean settingBean = SettingParam.getSettingBean();
		settingBean.setSupportTourist(isYes);
		SettingParam.updateSettingBean(settingBean);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
		/** 支持体验模式 */
		if (isYes && menuBeans.get(pos).getNameStr().equals(ty[1])) {
			isYes = false;
			saveChange();
		}

		/** 不支持体验模式 */
		if (!isYes && menuBeans.get(pos).getNameStr().equals(ty[0])) {
			isYes = true;
			saveChange();
		}

	}
}
